import Vue from 'vue'
import Router from 'vue-router'
import login from '@/pages/login/main.vue'//登录
import reg from '@/pages/login/reg.vue'//注册
import forget from '@/pages/login/forget.vue'//找回密码
import newproduct from '@/pages/newproduct/main.vue'//每日新品
import productlist from '@/pages/productlist/main.vue'//产品列表(一排两个)
import searchlist from '@/pages/productlist/list.vue'//搜索结果页
import commit from '@/pages/commit/main.vue'//全部评论页面
import order from '@/pages/order/main.vue'//确认订单
import ordershow from '@/pages/order/orderinfo.vue'//订单详情
import ordermsg from '@/pages/order/message.vue'//写评论
import orderlist from '@/pages/order/list'//列表
import mycommit from '@/pages/order/mycommit.vue'//我的评论
import addresslist from '@/pages/address/main.vue'//地址管理
import physical from '@/pages/address/physical.vue'//物流
import ferund from '@/pages/order/ferund.vue'//申请退款
import ferundInfo from '@/pages/order/ferundInfo.vue'//申请详情
import subClassproductlist from '@/pages/productlist/productlist.vue'//二级分类进入的产品列表
import setting from '@/pages/login/setting.vue'//个人设置
import findpwd from '@/pages/login/findpwd.vue'//找回密码
import about from '@/pages/commit/about.vue'//关于我们
import ordershowmsg from '@/pages/commit/ordershowmsg.vue'//查看我的评价
import orderstaus from '@/pages/order/orderstaus.vue'//支付详情
Vue.use(Router)

/*首页*/
import Category from '../pages/tabbar/Category'
import Self from '../pages/tabbar/Self'
import Home from '../pages/tabbar/Home'
import Cart from '../pages/tabbar/Cart'

/*商品详情*/
import GoodsDetail from '../pages/goodsdetail'

const router = new Router({
  routes: [{
    path: '/home',
    name: 'home',
    // alias: '/',
    component: Home,
    meta: {
      author: false
    }
  }, {
    path: '/category',
    name: 'category',
    component: Category,
    meta: {
      author: false
    }
  }, {
    path: '/cart',
    name: 'cart',
    alias: '/',
    component: Cart,
    meta: {
      author: false
    }
  }, {
    path: '/self',
    name: 'self',
    component: Self,
    meta: {
      author: true
    }
  }, {
    path: '/goodsdetail',
    name: 'goodsdetail',
    component: GoodsDetail,
    meta: {
      author: false
    }
  }, {
    path: '/login',
    name: 'login',
    component: login,
    meta: {
      author: false
    }
  }, {
    path: '/reg',
    name: 'reg',
    component: reg,
    meta: {
      author: false
    }
  }, {
    path: '/newproduct',
    name: 'newproduct',
    component: newproduct,
    meta: {
      author: false
    }
  }, {
    path: '/productlist',
    name: 'productlist',
    component: productlist,
    meta: {
      author: false
    }
  }, {
    path: '/searchlist',
    name: 'searchlist',
    component: searchlist,
    meta: {
      author: false
    }
  }, {
    path: '/commit',
    name: 'commit',
    component: commit,
    meta: {
      author: false
    }
  }, {
    path: '/order',
    name: 'order',
    component: order,
    meta: {
      author: true
    }
  }, {
    path: '/orderlist',
    name: 'orderlist',
    component: orderlist,
    meta: {
      author: true
    }
  }, {
    path: '/ordershow',
    name: 'ordershow',
    component: ordershow,
    meta: {
      author: true
    }
  }, {
    path: '/ordermsg',
    name: 'ordermsg',
    component: ordermsg,
    meta: {
      author: true
    }
  }, {
    path: '/mycommit',
    name: 'mycommit',
    component: mycommit,
    meta: {
      author: true
    }
  }, {
    path: '/addresslist',
    name: 'addresslist',
    component: addresslist,
    meta: {
      author: true
    }
  }, {
    path: '/physical',
    name: 'physical',
    component: physical,
    meta: {
      author: true
    }
  }, {
    path: '/ferund',
    name: 'ferund',
    component: ferund,
    meta: {
      author: true
    }
  }, {
    path: '/ferundInfo',
    name: 'ferundInfo',
    component: ferundInfo,
    meta: {
      author: true
    }
  }, {
    path: '/subClassproductlist',
    name: 'subClassproductlist',
    component: subClassproductlist,
    meta: {
      author: false
    }
  }, {
    path: '/setting',
    name: 'setting',
    component: setting,
    meta: {
      author: true
    }
  }, {
    path: '/findpwd',
    name: 'findpwd',
    component: findpwd,
    meta: {
      author: true
    }
  }, {
    path: '/forget',
    name: 'forget',
    component: forget,
    meta: {
      author: false
    }
  }, {
    path: '/about',
    name: 'about',
    component: about,
    meta: {
      author: false
    }
  }, {
    path: '/ordershowmsg',
    name: 'ordershowmsg',
    component: ordershowmsg,
    meta: {
      author: false
    }
  }, {
    path: '/orderstaus',
    name: 'orderstaus',
    component: orderstaus,
    meta: {
      author: false,
      title: '支付结果'
    }
  }
  ]
})

/**
 * 路由跳转前
 */
router.beforeEach((to, from, next) => {
  // 忽略授权的不进行验证,其他都要验证
  let {author = true} = to.meta;
  const userId = localStorage.getItem('token')
  const memberId = localStorage.getItem('memberId')
  if (author) {
    if (userId && memberId) {
      next(true);
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.path,
          from: from.path
        }
      })
    }
  } else {
    next(true);
  }
})

export default router;
